package com.ianswer.px.user.service.impl;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.ianswer.common.orm.PublicDao;
import com.ianswer.px.user.entity.UsersInterests;
import com.ianswer.px.user.service.UsersInterestsService;

@Service("usersInterestsService")
public class UsersInterestsServiceImpl implements UsersInterestsService {

	@Resource(name = "publicDao")
	private PublicDao publicDao;
	
	@Override
	public void save(UsersInterests u) {
		UsersInterests db = query(u.getUserId());
		if (db == null) {
			insert(u);
		} else {
			update(u);
		}
	}
	
	@Override
	public void insert(UsersInterests u) {
		publicDao.insert("usersInterests.insert", u);
	}
	
	@Override
	public void update(UsersInterests u) {
		publicDao.update("usersInterests.update", u);
	}

	@Override
	public UsersInterests query(long userId) {
		return publicDao.querySingle("usersInterests.get", userId);
	}

	
	@Override
	public UsersInterests get(long userId) {
		UsersInterests r = query(userId);
		if (r != null) {
			if (StringUtils.isEmpty(r.getBook()) &&
				StringUtils.isEmpty(r.getFood()) &&
				StringUtils.isEmpty(r.getGame()) &&
				StringUtils.isEmpty(r.getMovie()) &&
				StringUtils.isEmpty(r.getMusic()) &&
				StringUtils.isEmpty(r.getSports()) &&
				StringUtils.isEmpty(r.getStar()) &&
				StringUtils.isEmpty(r.getTel())) {
				return null;
			}
		}
		return r;
	}
}
